﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    public class DB_CharacterUpdateInfo
    {
        public int IN_SID;
        public int IN_PARTY_SID;// Data           :   this+0x4, Member, Type: int, 
        public int IN_GUILD_SID;// Data           :   this+0x8, Member, Type: int, 
        public int IN_PREV_GUILD_SID;// Data           :   this+0xC, Member, Type: int, 
        public int IN_X;// Data           :   this+0x10, Member, Type: int, 
        public int IN_Y;// Data           :   this+0x14, Member, Type: int, 
        public int IN_Z;// Data           :   this+0x18, Member, Type: int, 
        public int IN_LAYER;// Data           :   this+0x1C, Member, Type: int, 
        public int IN_LV;// Data           :   this+0x20, Member, Type: int, 
        public int IN_MAX_REACHED_LEVEL;// Data           :   this+0x24, Member, Type: int, 
        public long IN_EXP;// Data           :   this+0x28, Member, Type: __int64, 
        public long IN_LAST_DECREASED_EXP;// Data           :   this+0x30, Member, Type: __int64, 
        public int IN_HP;// Data           :   this+0x38, Member, Type: int, 
        public int IN_MP;// Data           :   this+0x3C, Member, Type: int, 
        public int IN_STAMINA;// Data           :   this+0x40, Member, Type: int, 
        public int IN_HAVOC;// Data           :   this+0x44, Member, Type: int, 
        public int IN_JOB;// Data           :   this+0x48, Member, Type: int, 
        public int IN_JOB_DEPTH;// Data           :   this+0x4C, Member, Type: int, 
        public int IN_JLV;// Data           :   this+0x50, Member, Type: int, 
        public long IN_JP;// Data           :   this+0x54, Member, Type: int, 
        public long IN_TOTAL_JP;// Data           :   this+0x58, Member, Type: int, 
        public int IN_JOB_0;// Data           :   this+0x5C, Member, Type: int, 
        public int IN_JOB_1;// Data           :   this+0x60, Member, Type: int, 
        public int IN_JOB_2;// Data           :   this+0x64, Member, Type: int, 
        public int IN_JLV_0;// Data           :   this+0x68, Member, Type: int, 
        public int IN_JLV_1;// Data           :   this+0x6C, Member, Type: int, 
        public int IN_JLV_2;// Data           :   this+0x70, Member, Type: int, 
        public decimal IN_IP;// Data           :   this+0x74, Member, Type: class c_fixed<10000>, 
        public int IN_CHA;// Data           :   this+0x7C, Member, Type: int, 
        public int IN_PKC;// Data           :   this+0x80, Member, Type: int, 
        public int IN_DKC;// Data           :   this+0x84, Member, Type: int, 
        public int IN_HUNTAHOLIC;// Data           :   this+0x88, Member, Type: int, 
        public long IN_GOLD;// Data           :   this+0x90, Member, Type: __int64, 
        public int IN_CHAOS;// Data           :   this+0x98, Member, Type: int, 
        public uint IN_SKIN_COLOR;// Data           :   this+0x9C, Member, Type: unsigned long, 
        public string IN_FLAG_LIST;// Data           :   this+0xA0, Member, Type: class std::basic_string<char,std::char_traits<char>,std::allocator<char> >, 
        public int[] IN_SUMMON = new int[6];// Data           :   this+0xBC, Member, Type: int[0x6], 
        public long[] IN_BELT = new long[6];// Data           :   this+0xD8, Member, Type: __int64[0x6], 
        public int IN_MAIN_SUMMON;// Data           :   this+0x108, Member, Type: int, 
        public int IN_SUB_SUMMON;// Data           :   this+0x10C, Member, Type: int, 
        public int IN_REMAIN_SUMMON_TIME;// Data           :   this+0x110, Member, Type: int, 
        public int IN_PET;// Data           :   this+0x114, Member, Type: int, 
        public int IN_REMAIN_CHAT_BLOCK_TIME;// Data           :   this+0x118, Member, Type: int, 
        public int IN_ADV_CHAT_COUNT;// Data           :   this+0x11C, Member, Type: int, 
        public int IN_AUTO_USED;// Data           :   this+0x120, Member, Type: int, 
        public long IN_GUILD_BLOCK_TIME;// Data           :   this+0x128, Member, Type: __int64, 
        public int IN_PKMODE;// Data           :   this+0x130, Member, Type: int, 
        public int IN_TP;// Data           :   this+0x130, Member, Type: int, 

    }

    /// <summary>
    /// DB_UpdateCharacter (smp_update_character)
    /// </summary>
    public class DB_UpdateCharacter : IDBProc
    {
        private Player pPlayer;
        private bool m_bOnlyCharacter;
        private DB_CharacterUpdateInfo CharacterInfo = new DB_CharacterUpdateInfo();

        private List<Player.FavorInfo> vFavorInfo = new List<Player.FavorInfo>();
        private List<State> vStateInfo = new List<State>();
        private List<State> vStateInfoRemovedByDeath = new List<State>();
        private uint[] nCoolTimes = new uint[40];

        public BaseObject m_CBObject = null;

        public DB_UpdateCharacter(Player _pPlayer, bool bOnlyCharacter)
        {
            this.m_bOnlyCharacter = bOnlyCharacter;
            this.pPlayer = _pPlayer;

            uint ct = Globals.GetArTime();

            int i;
            this.CharacterInfo.IN_SID = this.pPlayer.m_nUID;
            this.CharacterInfo.IN_PARTY_SID = this.pPlayer.m_nPartyId;
            this.CharacterInfo.IN_GUILD_SID = this.pPlayer.m_nGuildId;
            this.CharacterInfo.IN_PREV_GUILD_SID = this.pPlayer.GetPrevGuildID();
            this.CharacterInfo.IN_X = (int)this.pPlayer.mv.x;
            this.CharacterInfo.IN_Y = (int)this.pPlayer.mv.y;
            this.CharacterInfo.IN_Z = (int)this.pPlayer.mv.z;
            this.CharacterInfo.IN_LAYER = this.pPlayer.m_layer;
            this.CharacterInfo.IN_LV = this.pPlayer.GetLevel();
            this.CharacterInfo.IN_MAX_REACHED_LEVEL = this.pPlayer.GetMaxReachedLevel();
            this.CharacterInfo.IN_EXP = this.pPlayer.m_nEXP;
            this.CharacterInfo.IN_LAST_DECREASED_EXP = this.pPlayer.m_nLastDecreasedEXP;
            this.CharacterInfo.IN_HP = this.pPlayer.m_nHP;
            this.CharacterInfo.IN_MP = (int)this.pPlayer.m_fMP;
            this.CharacterInfo.IN_STAMINA = this.pPlayer.m_nStamina;
            this.CharacterInfo.IN_HAVOC = this.pPlayer.m_nHavoc;
            this.CharacterInfo.IN_JOB = this.pPlayer.GetJobId();
            this.CharacterInfo.IN_JOB_DEPTH = 0;
            this.CharacterInfo.IN_JLV = this.pPlayer.GetJobLevel();
            this.CharacterInfo.IN_JP = this.pPlayer.GetJobPoint();
            this.CharacterInfo.IN_TOTAL_JP = this.pPlayer.GetTotalJobPoint();
            this.CharacterInfo.IN_JOB_0 = this.pPlayer.GetPrevJobId(0);
            this.CharacterInfo.IN_JOB_1 = this.pPlayer.GetPrevJobId(1);
            this.CharacterInfo.IN_JOB_2 = this.pPlayer.GetPrevJobId(2);
            this.CharacterInfo.IN_JLV_0 = this.pPlayer.GetPrevJobLevel(0);
            this.CharacterInfo.IN_JLV_1 = this.pPlayer.GetPrevJobLevel(1);
            this.CharacterInfo.IN_JLV_2 = this.pPlayer.GetPrevJobLevel(2);
            this.CharacterInfo.IN_IP = (decimal) this.pPlayer.m_fImmoralPoint;
            this.CharacterInfo.IN_CHA = this.pPlayer.m_nCharisma;
            this.CharacterInfo.IN_PKC = this.pPlayer.m_nPKC;
            this.CharacterInfo.IN_DKC = this.pPlayer.m_nDKC;
            this.CharacterInfo.IN_HUNTAHOLIC = this.pPlayer.GetHuntaholicPoint();
            this.CharacterInfo.IN_GOLD = this.pPlayer.m_nGold;
            this.CharacterInfo.IN_CHAOS = this.pPlayer.m_nChaos;
            this.CharacterInfo.IN_SKIN_COLOR = this.pPlayer.GetSkinColor();
            this.CharacterInfo.IN_TP = this.pPlayer.m_nTP;
            this.pPlayer.EnumFlag(ref this.CharacterInfo.IN_FLAG_LIST);
            for (i = 0; i < 6; ++i)
            {
                if (this.pPlayer.m_aBindSummonCard[i] != null)
                {
                    this.CharacterInfo.IN_SUMMON[i] = this.pPlayer.m_aBindSummonCard[i].m_pSummon.m_nSID;
                }

                if (this.pPlayer.m_aBeltSlotCard[i] != null)
                {
                    this.CharacterInfo.IN_BELT[i] = this.pPlayer.m_aBeltSlotCard[i].m_Instance.UID;
                }
                else
                {
                    this.CharacterInfo.IN_BELT[i] = 0;
                }
            }

            this.CharacterInfo.IN_MAIN_SUMMON = 0;
            this.CharacterInfo.IN_SUB_SUMMON = 0;
            this.CharacterInfo.IN_REMAIN_SUMMON_TIME = 0;

            if (this.pPlayer.m_pMainSummon != null)
            {
                this.CharacterInfo.IN_MAIN_SUMMON = this.pPlayer.m_pMainSummon.GetSID();

                if (this.pPlayer.m_pSubSummon != null)
                {
                    this.CharacterInfo.IN_SUB_SUMMON = this.pPlayer.m_pSubSummon.GetSID();
                    this.CharacterInfo.IN_REMAIN_SUMMON_TIME = (int)(this.pPlayer.m_nNextUnSummonTime - ct);
                }
            }

            this.CharacterInfo.IN_PET = 0;

            if (this.pPlayer.m_pSummonedPet != null)
                this.CharacterInfo.IN_PET = this.pPlayer.m_pSummonedPet.GetSID();
            this.CharacterInfo.IN_REMAIN_CHAT_BLOCK_TIME = this.pPlayer.GetChatBlockTime();
            this.CharacterInfo.IN_ADV_CHAT_COUNT = this.pPlayer.m_nAdvChatCount;
            this.CharacterInfo.IN_AUTO_USED = this.pPlayer.m_bAutoUsed ? 1 : 0;
            this.CharacterInfo.IN_GUILD_BLOCK_TIME = this.pPlayer.GetGuildBlockTime();
            this.CharacterInfo.IN_PKMODE = this.pPlayer.m_bIsPK ? 1 : 0;
            if (!this.m_bOnlyCharacter)
            {
                foreach (Player.FavorInfo fi in this.pPlayer.m_vFavorInfo)
                {
                    this.vFavorInfo.Add(fi);
                }
                for(i = 0; i < 39; ++i)
                {
                    this.nCoolTimes[i] = pPlayer.m_nItemCoolTime[i];
                }
            }

            foreach (State st in this.pPlayer.m_vStateList)
            {
                this.vStateInfo.Add(st);
            }

            foreach (State st in this.pPlayer.m_vStateListRemovedByDeath)
            {
                this.vStateInfoRemovedByDeath.Add(st);
            }
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            updateCharacterInfo();
            if ( !this.m_bOnlyCharacter )
            {
                updateItemCoolTime();
                updateFavorInfo();
                updateStateInfo();
            }

            return true;
        }

        private void updateCharacterInfo()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "smp_update_character";
            cmd.CommandType = CommandType.StoredProcedure;

            DbParameter IN_SID = GameDBManager.Instance.CreateInParameter(cmd, "IN_SID", DbType.Int32, this.CharacterInfo.IN_SID);
            DbParameter IN_PARTY_SID = GameDBManager.Instance.CreateInParameter(cmd, "IN_PARTY_SID", DbType.Int32, this.CharacterInfo.IN_PARTY_SID);
//            DbParameter IN_GUILD_SID = GameDBManager.Instance.CreateInParameter(cmd, "IN_GUILD_SID", DbType.Int32, this.CharacterInfo.IN_GUILD_SID);
//            DbParameter IN_PREV_GUILD_SID = GameDBManager.Instance.CreateInParameter(cmd, "IN_PREV_GUILD_SID", DbType.Int32, this.CharacterInfo.IN_PREV_GUILD_SID);
            DbParameter IN_X = GameDBManager.Instance.CreateInParameter(cmd, "IN_X", DbType.Int32, this.CharacterInfo.IN_X);
            DbParameter IN_Y = GameDBManager.Instance.CreateInParameter(cmd, "IN_Y", DbType.Int32, this.CharacterInfo.IN_Y);
            DbParameter IN_Z = GameDBManager.Instance.CreateInParameter(cmd, "IN_Z", DbType.Int32, this.CharacterInfo.IN_Z);
            DbParameter IN_LAYER = GameDBManager.Instance.CreateInParameter(cmd, "IN_LAYER", DbType.Int32, this.CharacterInfo.IN_LAYER);
            DbParameter IN_LV = GameDBManager.Instance.CreateInParameter(cmd, "IN_LV", DbType.Int32, this.CharacterInfo.IN_LV);
            DbParameter IN_MAX_REACHED_LEVEL = GameDBManager.Instance.CreateInParameter(cmd, "IN_MAX_REACHED_LEVEL", DbType.Int32, this.CharacterInfo.IN_MAX_REACHED_LEVEL);
            DbParameter IN_EXP = GameDBManager.Instance.CreateInParameter(cmd, "IN_EXP", DbType.Int64, this.CharacterInfo.IN_EXP);
            DbParameter IN_LAST_DECREASED_EXP = GameDBManager.Instance.CreateInParameter(cmd, "IN_LAST_DECREASED_EXP", DbType.Int64, this.CharacterInfo.IN_LAST_DECREASED_EXP);
            DbParameter IN_HP = GameDBManager.Instance.CreateInParameter(cmd, "IN_HP", DbType.Int32, this.CharacterInfo.IN_HP);
            DbParameter IN_MP = GameDBManager.Instance.CreateInParameter(cmd, "IN_MP", DbType.Int32, this.CharacterInfo.IN_MP);
            DbParameter IN_STAMINA = GameDBManager.Instance.CreateInParameter(cmd, "IN_STAMINA", DbType.Int32, this.CharacterInfo.IN_STAMINA);
            DbParameter IN_HAVOC = GameDBManager.Instance.CreateInParameter(cmd, "IN_HAVOC", DbType.Int32, this.CharacterInfo.IN_HAVOC);
            DbParameter IN_JOB = GameDBManager.Instance.CreateInParameter(cmd, "IN_JOB", DbType.Int32, this.CharacterInfo.IN_JOB);
            DbParameter IN_JOB_DEPTH = GameDBManager.Instance.CreateInParameter(cmd, "IN_JOB_DEPTH", DbType.Int32, this.CharacterInfo.IN_JOB_DEPTH);
            DbParameter IN_JLV = GameDBManager.Instance.CreateInParameter(cmd, "IN_JLV", DbType.Int32, this.CharacterInfo.IN_JLV);
            DbParameter IN_JP = GameDBManager.Instance.CreateInParameter(cmd, "IN_JP", DbType.Int64, this.CharacterInfo.IN_JP);
            DbParameter IN_TOTAL_JP = GameDBManager.Instance.CreateInParameter(cmd, "IN_TOTAL_JP", DbType.Int64, this.CharacterInfo.IN_TOTAL_JP);
            DbParameter IN_TP = GameDBManager.Instance.CreateInParameter(cmd, "IN_TALENT_POINT", DbType.Int32, (byte)this.CharacterInfo.IN_TP);

            DbParameter IN_JOB_0 = GameDBManager.Instance.CreateInParameter(cmd, "IN_JOB_0", DbType.Int32, this.CharacterInfo.IN_JOB_0);
            DbParameter IN_JOB_1 = GameDBManager.Instance.CreateInParameter(cmd, "IN_JOB_1", DbType.Int32, this.CharacterInfo.IN_JOB_1);
            DbParameter IN_JOB_2 = GameDBManager.Instance.CreateInParameter(cmd, "IN_JOB_2", DbType.Int32, this.CharacterInfo.IN_JOB_2);
            DbParameter IN_JLV_0 = GameDBManager.Instance.CreateInParameter(cmd, "IN_JLV_0", DbType.Int32, this.CharacterInfo.IN_JLV_0);
            DbParameter IN_JLV_1 = GameDBManager.Instance.CreateInParameter(cmd, "IN_JLV_1", DbType.Int32, this.CharacterInfo.IN_JLV_1);
            DbParameter IN_JLV_2 = GameDBManager.Instance.CreateInParameter(cmd, "IN_JLV_2", DbType.Int32, this.CharacterInfo.IN_JLV_2);
            DbParameter IN_IP = GameDBManager.Instance.CreateInParameter(cmd, "IN_IP", DbType.Single, (float)this.CharacterInfo.IN_IP);
            DbParameter IN_CHA = GameDBManager.Instance.CreateInParameter(cmd, "IN_CHA", DbType.Int32, this.CharacterInfo.IN_CHA);
            DbParameter IN_PKC = GameDBManager.Instance.CreateInParameter(cmd, "IN_PKC", DbType.Int32, this.CharacterInfo.IN_PKC);
            DbParameter IN_DKC = GameDBManager.Instance.CreateInParameter(cmd, "IN_DKC", DbType.Int32, this.CharacterInfo.IN_DKC);
            DbParameter IN_HUNTAHOLIC = GameDBManager.Instance.CreateInParameter(cmd, "IN_HUNTAHOLIC_POINT", DbType.Int32, this.CharacterInfo.IN_HUNTAHOLIC);
            DbParameter IN_HUNTAHOLIC_ENTER_COUNT = GameDBManager.Instance.CreateInParameter(cmd, "IN_HUNTAHOLIC_ENTER_COUNT", DbType.Int32, 0);
            DbParameter IN_ETHEREAL_STONE_DURABILITY = GameDBManager.Instance.CreateInParameter(cmd, "IN_ETHEREAL_STONE_DURABILITY", DbType.Int32, 0);
            DbParameter IN_GOLD = GameDBManager.Instance.CreateInParameter(cmd, "IN_GOLD", DbType.Int64, this.CharacterInfo.IN_GOLD);
            DbParameter IN_CHAOS = GameDBManager.Instance.CreateInParameter(cmd, "IN_CHAOS", DbType.Int32, this.CharacterInfo.IN_CHAOS);
            DbParameter IN_SKIN_COLOR = GameDBManager.Instance.CreateInParameter(cmd, "IN_SKIN_COLOR", DbType.Int32, this.CharacterInfo.IN_SKIN_COLOR);
            DbParameter IN_FLAG_LIST = GameDBManager.Instance.CreateInParameter(cmd, "IN_FLAG_LIST", DbType.String, this.CharacterInfo.IN_FLAG_LIST);
            DbParameter IN_SUMMON_0 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SUMMON_0", DbType.Int32, this.CharacterInfo.IN_SUMMON[0]);
            DbParameter IN_SUMMON_1 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SUMMON_1", DbType.Int32, this.CharacterInfo.IN_SUMMON[1]);
            DbParameter IN_SUMMON_2 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SUMMON_2", DbType.Int32, this.CharacterInfo.IN_SUMMON[2]);
            DbParameter IN_SUMMON_3 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SUMMON_3", DbType.Int32, this.CharacterInfo.IN_SUMMON[3]);
            DbParameter IN_SUMMON_4 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SUMMON_4", DbType.Int32, this.CharacterInfo.IN_SUMMON[4]);
            DbParameter IN_SUMMON_5 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SUMMON_5", DbType.Int32, this.CharacterInfo.IN_SUMMON[5]);
            DbParameter IN_BELT_0 = GameDBManager.Instance.CreateInParameter(cmd, "IN_BELT_0", DbType.Int64, this.CharacterInfo.IN_BELT[0]);
            DbParameter IN_BELT_1 = GameDBManager.Instance.CreateInParameter(cmd, "IN_BELT_1", DbType.Int64, this.CharacterInfo.IN_BELT[1]);
            DbParameter IN_BELT_2 = GameDBManager.Instance.CreateInParameter(cmd, "IN_BELT_2", DbType.Int64, this.CharacterInfo.IN_BELT[2]);
            DbParameter IN_BELT_3 = GameDBManager.Instance.CreateInParameter(cmd, "IN_BELT_3", DbType.Int64, this.CharacterInfo.IN_BELT[3]);
            DbParameter IN_BELT_4 = GameDBManager.Instance.CreateInParameter(cmd, "IN_BELT_4", DbType.Int64, this.CharacterInfo.IN_BELT[4]);
            DbParameter IN_BELT_5 = GameDBManager.Instance.CreateInParameter(cmd, "IN_BELT_5", DbType.Int64, this.CharacterInfo.IN_BELT[5]);
            DbParameter IN_MAIN_SUMMON = GameDBManager.Instance.CreateInParameter(cmd, "IN_MAIN_SUMMON", DbType.Int32, this.CharacterInfo.IN_MAIN_SUMMON);
            DbParameter IN_SUB_SUMMON = GameDBManager.Instance.CreateInParameter(cmd, "IN_SUB_SUMMON", DbType.Int32, this.CharacterInfo.IN_SUB_SUMMON);
            DbParameter IN_REMAIN_SUMMON_TIME = GameDBManager.Instance.CreateInParameter(cmd, "IN_REMAIN_SUMMON_TIME", DbType.Int32, this.CharacterInfo.IN_REMAIN_SUMMON_TIME);
            DbParameter IN_PET = GameDBManager.Instance.CreateInParameter(cmd, "IN_PET", DbType.Int32, this.CharacterInfo.IN_PET);
            DbParameter IN_REMAIN_CHAT_BLOCK_TIME = GameDBManager.Instance.CreateInParameter(cmd, "IN_REMAIN_CHAT_BLOCK_TIME", DbType.Int32, this.CharacterInfo.IN_REMAIN_CHAT_BLOCK_TIME);
            DbParameter IN_ADV_CHAT_COUNT = GameDBManager.Instance.CreateInParameter(cmd, "IN_ADV_CHAT_COUNT", DbType.Int32, this.CharacterInfo.IN_ADV_CHAT_COUNT);
            DbParameter IN_AUTO_USED = GameDBManager.Instance.CreateInParameter(cmd, "IN_AUTO_USED", DbType.Int32, this.CharacterInfo.IN_AUTO_USED);
//            DbParameter IN_GUILD_BLOCK_TIME = GameDBManager.Instance.CreateInParameter(cmd, "IN_GUILD_BLOCK_TIME", DbType.Int32, this.CharacterInfo.IN_GUILD_BLOCK_TIME);
            DbParameter IN_PKMODE = GameDBManager.Instance.CreateInParameter(cmd, "IN_PKMODE", DbType.Byte, (byte)this.CharacterInfo.IN_PKMODE);

            conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_UpdateCharacter.updateCharacterInfo Exception: {0}", ex);
            }
            conn.Close();
        }

        private void updateItemCoolTime()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "smp_update_item_cool_time";
            cmd.CommandType = CommandType.StoredProcedure;

            GameDBManager.Instance.CreateInParameter(cmd, "IN_OWNER_UID", DbType.Int32, this.CharacterInfo.IN_SID);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_00", DbType.Int32, this.nCoolTimes[0]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_01", DbType.Int32, this.nCoolTimes[1]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_02", DbType.Int32, this.nCoolTimes[2]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_03", DbType.Int32, this.nCoolTimes[3]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_04", DbType.Int32, this.nCoolTimes[4]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_05", DbType.Int32, this.nCoolTimes[5]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_06", DbType.Int32, this.nCoolTimes[6]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_07", DbType.Int32, this.nCoolTimes[7]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_08", DbType.Int32, this.nCoolTimes[8]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_09", DbType.Int32, this.nCoolTimes[9]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_10", DbType.Int32, this.nCoolTimes[10]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_11", DbType.Int32, this.nCoolTimes[11]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_12", DbType.Int32, this.nCoolTimes[12]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_13", DbType.Int32, this.nCoolTimes[13]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_14", DbType.Int32, this.nCoolTimes[14]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_15", DbType.Int32, this.nCoolTimes[15]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_16", DbType.Int32, this.nCoolTimes[16]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_17", DbType.Int32, this.nCoolTimes[17]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_18", DbType.Int32, this.nCoolTimes[18]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_19", DbType.Int32, this.nCoolTimes[19]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_20", DbType.Int32, this.nCoolTimes[20]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_21", DbType.Int32, this.nCoolTimes[21]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_22", DbType.Int32, this.nCoolTimes[22]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_23", DbType.Int32, this.nCoolTimes[23]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_24", DbType.Int32, this.nCoolTimes[24]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_25", DbType.Int32, this.nCoolTimes[25]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_26", DbType.Int32, this.nCoolTimes[26]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_27", DbType.Int32, this.nCoolTimes[27]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_28", DbType.Int32, this.nCoolTimes[28]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_29", DbType.Int32, this.nCoolTimes[29]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_30", DbType.Int32, this.nCoolTimes[30]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_31", DbType.Int32, this.nCoolTimes[31]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_32", DbType.Int32, this.nCoolTimes[32]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_33", DbType.Int32, this.nCoolTimes[33]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_34", DbType.Int32, this.nCoolTimes[34]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_35", DbType.Int32, this.nCoolTimes[35]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_36", DbType.Int32, this.nCoolTimes[36]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_37", DbType.Int32, this.nCoolTimes[37]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_38", DbType.Int32, this.nCoolTimes[38]);
            GameDBManager.Instance.CreateInParameter(cmd, "COOL_TIME_39", DbType.Int32, this.nCoolTimes[39]);


            conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_UpdateCharacter.updateItemCoolTime Exception: {0}", ex);
            }
            conn.Close();
        }

        private void updateFavorInfo()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "smp_update_favor";
            cmd.CommandType = CommandType.StoredProcedure;

            DbParameter IN_OWNER_UID = GameDBManager.Instance.CreateInParameter(cmd, "IN_OWNER_ID", DbType.Int32, this.CharacterInfo.IN_SID);
            DbParameter IN_FAVOR_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_FAVOR_ID", DbType.Int32, this.nCoolTimes[0]);
            DbParameter IN_FAVOR = GameDBManager.Instance.CreateInParameter(cmd, "IN_FAVOR", DbType.Int32, this.nCoolTimes[1]);


            conn.Open();

            try
            {
                foreach (Player.FavorInfo fi in this.vFavorInfo)
                {
                    IN_OWNER_UID.Value = this.CharacterInfo.IN_SID;
                    IN_FAVOR_ID.Value = fi.favor_id;
                    IN_FAVOR.Value = fi.favor;
                    cmd.Prepare();
                    cmd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_UpdateCharacter.updateFavorInfo Exception: {0}", ex);
            }
            conn.Close();

        }

        private void updateStateInfo()
        {
            bool bIsHolded;             // 43   2Bh
            DbCommand clear_cmd;        //       = _com_ptr_t<_com_IIID<_Command,&_GUID_b08400bd_f9d1_4d02_b856_71d5dba123e9> > ptr  30h
            uint ct;                    // 96   60h
            DbCommand update_cmd;       //      = _com_ptr_t<_com_IIID<_Command,&_GUID_b08400bd_f9d1_4d02_b856_71d5dba123e9> > ptr  6Ch
            DbCommand insert_cmd;       //      = _com_ptr_t<_com_IIID<_Command,&_GUID_b08400bd_f9d1_4d02_b856_71d5dba123e9> > ptr  70h
            bool bInsert;               // 124  7Ch

//Data           :   ebp Relative, [0000007C], Param, Type: struct DBConnection &, db
// Data           :   ebp Relative, [FFFFFE40], Local, Type: class std::_Vector_iterator<int,std::allocator<int> >, itStateID
// Data           :   ebp Relative, [0000006C], Local, Type: class _com_ptr_t<_com_IIID<_Command,&_GUID_b08400bd_f9d1_4d02_b856_71d5dba123e9> >, update_cmd
// Data           :   ebp Relative, [00000030], Local, Type: class _com_ptr_t<_com_IIID<_Command,&_GUID_b08400bd_f9d1_4d02_b856_71d5dba123e9> >, clear_cmd
// Data           :   ebp Relative, [00000058], Local, Type: class std::_Vector_iterator<StructState,std::allocator<StructState> >, it
// Data           :   ebp Relative, [00000070], Local, Type: class _com_ptr_t<_com_IIID<_Command,&_GUID_b08400bd_f9d1_4d02_b856_71d5dba123e9> >, insert_cmd


            DbConnection conn = GameDBManager.Instance.CreateConnection();

            // create our empty commands
            
            clear_cmd = conn.CreateCommand();

            clear_cmd.CommandText = "dbo.smp_clear_state_list";
            clear_cmd.CommandType = CommandType.StoredProcedure;

            DbParameter clear_IN_OWNER_ID = GameDBManager.Instance.CreateInParameter(clear_cmd, "IN_OWNER_ID", DbType.Int32,pPlayer.GetSID());
            DbParameter clear_IN_SUMMON_ID = GameDBManager.Instance.CreateInParameter(clear_cmd, "IN_SUMMON_ID", DbType.Int32,0);

            update_cmd = conn.CreateCommand();
            update_cmd.CommandText = "dbo.smp_update_state";
            update_cmd.CommandType = CommandType.StoredProcedure;

            DbParameter update_IN_SID = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_SID", DbType.Int32);
            DbParameter update_IN_CODE = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_CODE", DbType.Int32);
            DbParameter update_IN_LEVEL = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_LEVEL", DbType.Int32);
            DbParameter update_IN_DURATION = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_DURATION", DbType.Int32);
            DbParameter update_IN_REMAIN_TIME = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_REMAIN_TIME", DbType.Int32);
            DbParameter update_IN_BASE_DAMAGE = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_BASE_DAMAGE", DbType.Int32);
            DbParameter update_IN_REMAIN_FIRE_TIME = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_REMAIN_FIRE_TIME", DbType.Int32);
            DbParameter update_IN_STATE_VALUE = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_STATE_VALUE", DbType.Int32);
            DbParameter update_IN_STATE_STRING_VALUE = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_STATE_STRING_VALUE", DbType.String);
            DbParameter update_IN_ENABLE = GameDBManager.Instance.CreateInParameter(update_cmd, "IN_ENABLE", DbType.Int32);

            insert_cmd = conn.CreateCommand();
            insert_cmd.CommandText = "dbo.smp_insert_state";
            insert_cmd.CommandType = CommandType.StoredProcedure;

            DbParameter insert_OUT_SID = GameDBManager.Instance.CreateOutParameter(insert_cmd, "OUT_SID", DbType.Int32);
            DbParameter insert_IN_OWNER_ID = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_OWNER_ID", DbType.Int32,pPlayer.GetSID());
            DbParameter insert_IN_SUMMON_ID = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_SUMMON_ID", DbType.Int32,0);
            DbParameter insert_IN_CODE = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_CODE", DbType.Int32);
            DbParameter insert_IN_LEVEL = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_LEVEL", DbType.Int32);
            DbParameter insert_IN_DURATION = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_DURATION", DbType.Int32);
            DbParameter insert_IN_REMAIN_TIME = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_REMAIN_TIME", DbType.Int32);
            DbParameter insert_IN_BASE_DAMAGE = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_BASE_DAMAGE", DbType.Int32);
            DbParameter insert_IN_REMAIN_FIRE_TIME = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_REMAIN_FIRE_TIME", DbType.Int32);
            DbParameter insert_IN_STATE_VALUE = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_STATE_VALUE", DbType.Int32);
            DbParameter insert_IN_STATE_STRING_VALUE = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_STATE_STRING_VALUE", DbType.String);
            DbParameter insert_IN_ENABLE = GameDBManager.Instance.CreateInParameter(insert_cmd, "IN_ENABLE", DbType.Int32);

            // clear our states first
            clear_IN_OWNER_ID.Value = this.pPlayer.GetSID();
            clear_IN_SUMMON_ID.Value = 0;
            clear_cmd.Prepare();

            conn.Open();

            try
            {
                clear_cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_UpdateCharacter.updateStateInfo Exception: {0}", ex);
            }
// 
//             GameDBManager.Instance.CreateInParameter(clear_cmd, "IN_OWNER_ID", DbType.Int32, this.pPlayer.GetSID());
//             GameDBManager.Instance.CreateInParameter(clear_cmd, "IN_SUMMON_ID", DbType.Int32, 0);



            ct = Globals.GetArTime();
            bInsert = false;

            foreach (State s in this.vStateInfo)
            {
                bInsert = false;
                if (!this.pPlayer.m_vStateIDList.Contains(s.m_nUID))
                    bInsert = true;

                if (bInsert)
                {
                    insert_IN_CODE.Value = (int)s.m_nCode;
                    insert_IN_LEVEL.Value = s.m_nLevel[0];
                    if(s.m_nEndTime[0] <= ct)
                        insert_IN_DURATION.Value = 0;
                    else
                        insert_IN_DURATION.Value = (int)(s.m_nEndTime[0] - s.m_nStartTime[0]);

                    if(s.m_nEndTime[0] == 0xffffffff)
                        insert_IN_REMAIN_TIME.Value = -1;
                    else
                        insert_IN_REMAIN_TIME.Value = (int)Math.Max(s.m_nEndTime[0] - ct,0);
                    insert_IN_BASE_DAMAGE.Value = s.m_nBaseDamage[0];

/*
                    if (s.m_nEndTime[1] > ct || s.m_nEndTime[1] == 0xffffffff)
                    {
                        insert_IN_LEVEL_2.Value = s.m_nLevel[1];
                        if(s.m_nEndTime[1] <= ct)
                            insert_IN_DURATION_2.Value = 0;
                        else
                            insert_IN_DURATION_2.Value = (int)(s.m_nEndTime[1] - s.m_nStartTime[1]);
                        if(s.m_nEndTime[1] == 0xffffffff)
                            insert_IN_REMAIN_TIME_2.Value = -1;
                        else
                            insert_IN_REMAIN_TIME_2.Value = (int)Math.Max(s.m_nEndTime[1] - ct,0);
                        insert_IN_BASE_DAMAGE_2.Value = s.m_nBaseDamage[1];

                    }
                    else
                    {
                        insert_IN_LEVEL_2.Value = 0;
                        insert_IN_DURATION_2.Value = 0;
                        insert_IN_REMAIN_TIME_2.Value = 0;
                        insert_IN_BASE_DAMAGE_2.Value = 0;
                    }
                    if (s.m_nEndTime[2] > ct || s.m_nEndTime[2] == 0xffffffff)
                    {
                        insert_IN_LEVEL_3.Value = s.m_nLevel[2];
                        if(s.m_nEndTime[2] <= ct)
                            insert_IN_DURATION_3.Value = 0;
                        else
                            insert_IN_DURATION_3.Value = (int)(s.m_nEndTime[2] - s.m_nStartTime[2]);
                        if(s.m_nEndTime[2] == 0xffffffff)
                            insert_IN_REMAIN_TIME_3.Value = -1;
                        else
                            insert_IN_REMAIN_TIME_3.Value = (int)Math.Max(s.m_nEndTime[2] - ct,0);
                        insert_IN_BASE_DAMAGE_3.Value = s.m_nBaseDamage[2];

                    }
                    else
                    {
                        insert_IN_LEVEL_3.Value = 0;
                        insert_IN_DURATION_3.Value = 0;
                        insert_IN_REMAIN_TIME_3.Value = 0;
                        insert_IN_BASE_DAMAGE_3.Value = 0;
                    }

*/
                    StateInfo si = GameContent.GetStateInfo((int)s.m_nCode);
                    insert_IN_REMAIN_FIRE_TIME.Value = (int)(s.m_nLastProcessedTime + (100 * (uint)si.fire_interval - ct));
                    insert_IN_STATE_VALUE.Value = s.m_nStateValue;
                    insert_IN_STATE_STRING_VALUE.Value = s.m_szStateValue;
                    insert_IN_ENABLE.Value = 1;
                    insert_cmd.Prepare();
                    try
                    {
                        insert_cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Globals.Log.Error("DB_UpdateCharacter.updateStateInfo (Insert) Exception: {0}", ex);
                    }

                    this.pPlayer.m_vStateIDList.Add((int)insert_OUT_SID.Value);
                }
                else
                {
                    update_IN_SID.Value = s.m_nUID;
                    update_IN_CODE.Value = s.m_nCode;

                    bIsHolded = s.IsHolded();
                    if (bIsHolded)
                    {
                        s.ReleaseRemainDuration();
                    }

                    update_IN_LEVEL.Value = s.m_nLevel[0];
                    if(s.m_nEndTime[0] <= ct)
                        update_IN_DURATION.Value = 0;
                    else
                        update_IN_DURATION.Value = (int)(s.m_nEndTime[0] - s.m_nStartTime[0]);

                    if(s.m_nEndTime[0] == 0xffffffff)
                        update_IN_REMAIN_TIME.Value = -1;
                    else
                        update_IN_REMAIN_TIME.Value = (int)Math.Max(s.m_nEndTime[0] - ct, 0);
                    update_IN_BASE_DAMAGE.Value = s.m_nBaseDamage[0];

/*
                    if (s.m_nEndTime[1] > ct || s.m_nEndTime[1] == 0xffffffff)
                    {
                        update_IN_LEVEL_2.Value = s.m_nLevel[1];
                        if(s.m_nEndTime[1] <= ct)
                            update_IN_DURATION_2.Value = 0;
                        else
                            update_IN_DURATION_2.Value = (int)(s.m_nEndTime[1] - s.m_nStartTime[1]);
                        if(s.m_nEndTime[1] == 0xffffffff)
                            update_IN_REMAIN_TIME_2.Value = -1;
                        else
                            update_IN_REMAIN_TIME_2.Value = (int)Math.Max(s.m_nEndTime[1] - ct, 0);
                        update_IN_BASE_DAMAGE_2.Value = s.m_nBaseDamage[1];

                    }
                    else
                    {
                        update_IN_LEVEL_2.Value = 0;
                        update_IN_DURATION_2.Value = 0;
                        update_IN_REMAIN_TIME_2.Value = 0;
                        update_IN_BASE_DAMAGE_2.Value = 0;
                    }
                    if (s.m_nEndTime[2] > ct || s.m_nEndTime[2] == 0xffffffff)
                    {
                        update_IN_LEVEL_3.Value = s.m_nLevel[2];
                        if(s.m_nEndTime[2] <= ct)
                            update_IN_DURATION_3.Value = 0;
                        else
                            update_IN_DURATION_3.Value = (int)(s.m_nEndTime[2] - s.m_nStartTime[2]);
                        if(s.m_nEndTime[2] == 0xffffffff)
                            update_IN_REMAIN_TIME_3.Value = -1;
                        else
                            update_IN_REMAIN_TIME_3.Value = (int)Math.Max(s.m_nEndTime[2] - ct, 0);
                        update_IN_BASE_DAMAGE_3.Value = s.m_nBaseDamage[2];

                    }
                    else
                    {
                        update_IN_LEVEL_3.Value = 0;
                        update_IN_DURATION_3.Value = 0;
                        update_IN_REMAIN_TIME_3.Value = 0;
                        update_IN_BASE_DAMAGE_3.Value = 0;
                    }
*/

                    StateInfo si = GameContent.GetStateInfo((int)s.m_nCode);
                    update_IN_REMAIN_FIRE_TIME.Value = (int)(s.m_nLastProcessedTime + (100 * (uint)si.fire_interval - ct));
                    update_IN_STATE_VALUE.Value = s.m_nStateValue;
                    update_IN_STATE_STRING_VALUE.Value = s.m_szStateValue;
                    update_IN_ENABLE.Value = 1;
                    update_cmd.Prepare();
                    try
                    {
                        update_cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Globals.Log.Error("DB_UpdateCharacter.updateStateInfo (Insert) Exception: {0}", ex);
                    }
                }
            }

            foreach (State s in this.vStateInfoRemovedByDeath)
            {
                bInsert = false;
                if (!this.pPlayer.m_vStateIDList.Contains(s.m_nUID))
                    bInsert = true;

                if (bInsert)
                {
                    insert_IN_CODE.Value = s.m_nCode;
                    insert_IN_LEVEL.Value = s.m_nLevel[0];
                    if(s.m_nEndTime[0] <= ct)
                        insert_IN_DURATION.Value = 0;
                    else
                        insert_IN_DURATION.Value = (int)(s.m_nEndTime[0] - s.m_nStartTime[0]);

                    if(s.m_nEndTime[0] == 0xffffffff)
                        insert_IN_REMAIN_TIME.Value = -1;
                    else
                        insert_IN_REMAIN_TIME.Value = (int)Math.Max(s.m_nEndTime[0] - ct, 0);
                    insert_IN_BASE_DAMAGE.Value = s.m_nBaseDamage[0];

/*
                    if (s.m_nEndTime[1] > ct || s.m_nEndTime[1] == 0xffffffff)
                    {
                        insert_IN_LEVEL.Value = s.m_nLevel[1];
                        if(s.m_nEndTime[1] <= ct)
                            insert_IN_DURATION_2.Value = 0;
                        else
                            insert_IN_DURATION_2.Value = (int)(s.m_nEndTime[1] - s.m_nStartTime[1]);
                        if(s.m_nEndTime[1] == 0xffffffff)
                            insert_IN_REMAIN_TIME_2.Value = -1;
                        else
                            insert_IN_REMAIN_TIME_2.Value = (int)Math.Max(s.m_nEndTime[1] - ct, 0);
                        insert_IN_BASE_DAMAGE_2.Value = s.m_nBaseDamage[1];

                    }
                    else
                    {
                        insert_IN_LEVEL_2.Value = 0;
                        insert_IN_DURATION_2.Value = 0;
                        insert_IN_REMAIN_TIME_2.Value = 0;
                        insert_IN_BASE_DAMAGE_2.Value = 0;
                    }
                    if (s.m_nEndTime[2] > ct || s.m_nEndTime[2] == 0xffffffff)
                    {
                        insert_IN_LEVEL_3.Value = s.m_nLevel[2];
                        if(s.m_nEndTime[2] <= ct)
                            insert_IN_DURATION_3.Value = 0;
                        else
                            insert_IN_DURATION_3.Value = (int)(s.m_nEndTime[2] - s.m_nStartTime[2]);
                        if(s.m_nEndTime[2] == 0xffffffff)
                            insert_IN_REMAIN_TIME_3.Value = -1;
                        else
                            insert_IN_REMAIN_TIME_3.Value = (int)Math.Max(s.m_nEndTime[2] - ct, 0);
                        insert_IN_BASE_DAMAGE_3.Value = s.m_nBaseDamage[2];

                    }
                    else
                    {
                        insert_IN_LEVEL_3.Value = 0;
                        insert_IN_DURATION_3.Value = 0;
                        insert_IN_REMAIN_TIME_3.Value = 0;
                        insert_IN_BASE_DAMAGE_3.Value = 0;
                    }
*/

                    StateInfo si = GameContent.GetStateInfo((int)s.m_nCode);
                    insert_IN_REMAIN_FIRE_TIME.Value = (int)(s.m_nLastProcessedTime + (100 * (uint)si.fire_interval - ct));
                    insert_IN_STATE_VALUE.Value = s.m_nStateValue;
                    insert_IN_STATE_STRING_VALUE.Value = s.m_szStateValue;
                    insert_IN_ENABLE.Value = 2;
                    insert_cmd.Prepare();
                    try
                    {
                        insert_cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Globals.Log.Error("DB_UpdateCharacter.updateStateInfo (Insert) Exception: {0}", ex);
                    }

                    this.pPlayer.m_vStateIDList.Add((int)insert_OUT_SID.Value);
                }
                else
                {
                    update_IN_SID.Value = s.m_nUID;
                    update_IN_CODE.Value = s.m_nCode;

                    bIsHolded = s.IsHolded();
                    if (bIsHolded)
                    {
                        s.ReleaseRemainDuration();
                    }

                    insert_IN_LEVEL.Value = s.m_nLevel[0];
                    if(s.m_nEndTime[0] <= ct)
                        update_IN_DURATION.Value = 0;
                    else
                        update_IN_DURATION.Value = s.m_nEndTime[0] - s.m_nStartTime[0];

                    if(s.m_nEndTime[0] == 0xffffffff)
                        update_IN_REMAIN_TIME.Value = -1;
                    else
                        update_IN_REMAIN_TIME.Value = (int)Math.Max(s.m_nEndTime[0] - ct, 0);
                    insert_IN_BASE_DAMAGE.Value = s.m_nBaseDamage[0];

/*
                    if (s.m_nEndTime[1] > ct || s.m_nEndTime[1] == 0xffffffff)
                    {
                        update_IN_LEVEL_2.Value = s.m_nLevel[1];
                        if(s.m_nEndTime[1] <= ct)
                            update_IN_DURATION_2.Value = 0;
                        else
                            update_IN_DURATION_2.Value = s.m_nEndTime[1] - s.m_nStartTime[1];
                        if(s.m_nEndTime[1] == 0xffffffff)
                            update_IN_REMAIN_TIME_2.Value = -1;
                        else
                            update_IN_REMAIN_TIME_2.Value = (int)Math.Max(s.m_nEndTime[1] - ct, 0);
                        update_IN_BASE_DAMAGE_2.Value = s.m_nBaseDamage[1];

                    }
                    else
                    {
                        update_IN_LEVEL_2.Value = 0;
                        update_IN_DURATION_2.Value = 0;
                        update_IN_REMAIN_TIME_2.Value = 0;
                        update_IN_BASE_DAMAGE_2.Value = 0;
                    }
                    if (s.m_nEndTime[2] > ct || s.m_nEndTime[2] == 0xffffffff)
                    {
                        update_IN_LEVEL_3.Value = s.m_nLevel[2];
                        if(s.m_nEndTime[2] <= ct)
                            update_IN_DURATION_3.Value = 0;
                        else
                            update_IN_DURATION_3.Value = s.m_nEndTime[2] - s.m_nStartTime[2];
                        if(s.m_nEndTime[2] == 0xffffffff)
                            update_IN_REMAIN_TIME_3.Value = -1;
                        else
                            update_IN_REMAIN_TIME_3.Value = (int)Math.Max(s.m_nEndTime[2] - ct, 0);
                        update_IN_BASE_DAMAGE_3.Value = s.m_nBaseDamage[2];

                    }
                    else
                    {
                        update_IN_LEVEL_3.Value = 0;
                        update_IN_DURATION_3.Value = 0;
                        update_IN_REMAIN_TIME_3.Value = 0;
                        update_IN_BASE_DAMAGE_3.Value = 0;
                    }

*/
                    StateInfo si = GameContent.GetStateInfo((int)s.m_nCode);
                    update_IN_REMAIN_FIRE_TIME.Value = (int)(s.m_nLastProcessedTime + (100 * (uint)si.fire_interval - ct));
                    update_IN_STATE_VALUE.Value = s.m_nStateValue;
                    update_IN_STATE_STRING_VALUE.Value = s.m_szStateValue;
                    update_IN_ENABLE.Value = 2;
                    update_cmd.Prepare();
                    try
                    {
                        insert_cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Globals.Log.Error("DB_UpdateCharacter.updateStateInfo (Insert) Exception: {0}", ex);
                    }
                }
            }

            conn.Close();
        }

    }


}
